package com.yonyou.pmclouds.picture.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.picture.entity.PictureToNCCVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 提供给NCC的业务单据图片查询接口
 *
 */
@RestController
@RequestMapping({"/outter/picture"})
public class PictureController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private PictureRemoteService pictureRemoteService;

    @Autowired
    private StorageFactory storageFactory;

    @RequestMapping("/queryByTypePks")
    public List<PictureToNCCVO> queryByTypePks(@RequestBody Map<String, String[]> typePksMap,HttpServletRequest request) throws BusinessException{
        if(typePksMap == null || typePksMap.size() <= 0){
            return null;
        }

        List<PictureToNCCVO> resultList = pictureRemoteService.queryByPksTONCC(typePksMap);
        if(resultList == null || resultList.size() <= 0){
            return null;
        }
        for (PictureToNCCVO picVO : resultList){
            if(ArrayUtils.isEmpty(picVO.getUrls()))
                continue;
            String[] tempUrls = new String[picVO.getUrls().length];
            for (int i = 0 ; i < picVO.getUrls().length ; i++){
                tempUrls[i] = storageFactory.getStorageUtil().signUrlForSingleAppPic(picVO.getUrls()[i], null, request);
            }
            picVO.setUrls(tempUrls);
        }

        return resultList;

    }
}
